<?php


namespace app\admin\controller;


use app\admin\service\RbacService;
use app\common\controller\CommonController;
use app\common\exception\ParameterFailedException;
use app\common\exception\NotFoundException;
use app\common\model\XXXXXModel;
use app\common\service\Lang;
use app\common\validate\XXXXXValidate;
use think\facade\Db;
use think\Request

class XXXXXController extends CommonController
{
    public function getXXXXXList(Request $request)
    {
        $searchParams = self::getParam([]);
        $where        = [];
        foreach ($searchParams as $key => $value) {
            if (is_array($value) && $key === 'create_time') {
                $where[] = [
                    $key,
                    'between',
                    $value,
                ];
                continue;
            }
            if (strpos($value, '%') !== false) {
                $where[] = [
                    $key,
                    'like',
                    $value,
                ];
            }
            else {
                $where[] = [
                    $key,
                    '=',
                    $value,
                ];
            }
        }
        $where = [];
        $model = (new XXXXXModel())->with(
            []
        )->order(
            [
                //            'sea_waybill_number' => 'desc',
                //            'case_number'        => 'desc',
                'create_time' => 'desc',
            ]
        )->where($where)->getXXXXXListPaginate([], '*', [], $this->page, $this->limit);
        return retJson(Lang::GETDone, $model->toArray());
    }

    public function postXXXXX(Request $request)
    {

        $params      = self::getParam(
            [
                'nickname',
            ]
        );
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $oldModel = (new XXXXXModel())->where('id_card', $params['id_card'])->find();
            if ($oldModel) {
                throw new ParameterFailedException(['msg' => '已存在该数据']);
            }

            $model           = new XXXXXModel();
            $model->nickname = $params['nickname'];

            $model->save();

            Db::commit();
            return retJson(Lang::POSTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function putXXXXX(Request $request)
    {

        $params      = self::getParam(
            [
                'id',
                'xxxxx',
            ]
        );
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = XXXXXModel::where('id', $params['id'])->find();
            if (!$model) {
                throw new NotFoundException();
            }

            $carModel = YYYYYModel::where('driver_id', $params['id'])->find();
            if ($carModel) {
                throw new NotFoundException(['msg' => '该xx已被yy绑定，请解除绑定后继续操作']);
            }

            $model->nickname = $params['nickname'];
            $model->save();

            Db::commit();
            return retJson(Lang::PUTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function deleteXXXXX(Request $request)
    {

        $params      = self::getParam(['id']);
        (new XXXXXValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = XXXXXModel::where('id', $params['id'])->find();
            if (!$model) {
                throw new NotFoundException();
            }


            $otherModel = OtherModel::where('id', $params['id'])->find();
            if (!$otherModel) {
                throw new NotFoundException();
            }

            $model->delete_time = NOW_TIME;
            $model->save();

            Db::commit();

            return retJson(Lang::DELETEDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }
}